﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Structure;
using SdmxMl.Common;
using SdmxMl.Manager;
namespace ActivityMgr.HGDE
{
    public partial class DlgOfsCorresp : Form
    {
        private Communes municipalities;
        private DataTable tbCorr;

        public DlgOfsCorresp()
        {
            InitializeComponent();

            try
            {
                string gdeFullId = "CH1_RN+HCL_HGDE_HIST+1.0";
                municipalities = new Communes(gdeFullId);

                tbCorr = CreateCorrTable();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dateTimePickerEnd_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ofsCode = textBoxOfsCode.Text.Trim();
            if (ofsCode.Length > 0)
            {
                tbCorr = CreateCorrTable();

                List<ValidityCode> OfsList = municipalities.validityList.FindOfs(ofsCode);

                // Take older for starting
                if (OfsList.Count > 1)
                {
                    List<ValidityCode> OfsListFirst = new List<ValidityCode>();
                    OfsListFirst.Add(OfsList[0]);
                    OfsList = OfsListFirst;
                }

                makeLinksRecurse(OfsList);

                dataGridViewCorr.DataSource = tbCorr;

            }

        }

        private void makeLinksRecurse(List<ValidityCode> list)
        {
            foreach (ValidityCode vc in list)
            {
                DataRow row = tbCorr.Rows.Add(new object[] 
                { 
                    vc.OfsCode,
                    vc.DtFrom.ToString("yyyy.MM.dd"),
                     vc.DtTo.ToString("yyyy.MM.dd"),
                    string.Empty
                });

                if (vc.NextList != null)
                {
                    makeLinksRecurse(vc.NextList);
                }
            }
        }

        public DataTable CreateCorrTable()
        {
            DataTable t = new DataTable();
            t.Columns.Add(new DataColumn("OfsCode", typeof(string)));
            t.Columns.Add(new DataColumn("From", typeof(string)));
            t.Columns.Add(new DataColumn("To", typeof(string)));
            t.Columns.Add(new DataColumn("NewCode", typeof(string)));

            return t;
        }


    }
}
